package com.sprometheus.event;

import com.sprometheus.core.pojo.GlobalExceptionBO;
import com.sprometheus.send.SendMessage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Map;

/**
 * @author 风白羽
 * @date 2024/9/5 下午4:56
 */
@Component
public class GlobalExpEventListener {

    private static final Logger logger = LoggerFactory.getLogger(GlobalExpEventListener.class);
    @Resource
    private Map<String, SendMessage> sendMessageMap;

    @EventListener(GlobalExpEvent.class)
    public void sendEventListener(GlobalExpEvent event) {
        logger.info("已经收到监听事件，消费者...: {}", event.getMessage());
        GlobalExceptionBO message = event.getMessage();
        String groupUrl = message.getGroupUrl();
        String scene = message.getScene();
        message.setGroupUrl(groupUrl);
        message.setScene(scene);
        SendMessage sendMessage = sendMessageMap.get(message.getSendChannel());
        boolean success = sendMessage.send(message);
        if (!success) {
            logger.error("推送消息失败：url:{},环境:{}", groupUrl, scene);
        }
    }

}
